#!/usr/bin/env python
# -*- coding: utf-8 -*-

from importall import lib
from concurrent.futures import ThreadPoolExecutor, as_completed
import sys


def run(executor: ThreadPoolExecutor, cache_size: int = 20):
    for file in range(16):  # range(16)
        data = 'data/fiu-meta/ug-filesrv/ug-filesrv.%d-2014-09-16.csv' % (file)
        for algo in (0, 1, 3,):  # (0, 1, 3)
            args = '%d %s %d' % (algo, data, cache_size)
            if file == 9:
                args = args + ' --RC=0'  # do not repeat
            out = 'output/ug-filesrv.%d/%d/%d.txt' % (file, algo, cache_size)
            future = executor.submit(lib.cas_exec, 'scratch/fiu', args, out)
            if file == 9:
                # wait until complete
                for f in as_completed({future}):
                    print('ug-filesrv', file, algo, 'done')
        # executor.submit(lib.cas_exec, 'scratch/opt', '4 %s %d' % (data, cache_size),
        #                 'output/ug-filesrv.%d/4/%d.txt' % (file, cache_size))
    return


if __name__ == '__main__':
    with ThreadPoolExecutor(max_workers=4) as executor:
        cache_size = int(sys.argv[1]) if len(sys.argv) > 1 else 20
        run(executor, cache_size)
    print('ug-filesrv finish')
